package com.turing.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.turing.data.dao.review.ReviewContentMapper;
import com.turing.data.inner.FileTaskInnerService;
import com.turing.data.model.domain.review.ReviewContent;
import com.turing.data.service.element.ElementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("script")
public class ScriptController {

    private static final Log logger = LogFactory.getLog(ScriptController.class);

    @Autowired
    private ReviewContentMapper reviewContentMapper;

    @Autowired
    private ElementService elementService;

    @Autowired
    private FileTaskInnerService fileTaskInnerService;

    /*
     * 有些review_content数据没有对应的要素类型，需要脚本补充进去
     * batchNum  每次处理数量
     * */
    @GetMapping("doRefreshNullReviewEle")
    public String doRefreshNullReviewEle(@RequestParam("batchNum") Integer batchNum) {

        QueryWrapper<ReviewContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.or().eq("id", null)
                .or().eq("id", "null")
                .or().eq("id", "")
                .orderByAsc("updated_time");
        PageHelper.startPage(0, batchNum);
        List<ReviewContent> reviewContents = reviewContentMapper.selectList(queryWrapper);

        int count = 0;
        if (reviewContents != null && reviewContents.size() > 0) {
            for (ReviewContent reviewContent : reviewContents) {
                /*String taskSn = reviewContent.getTaskSn();

                String templateId = fileTaskInnerService.getTaskTemplate(taskSn);

                String content = reviewContent.getContent();
                JSONArray conArr = JSONArray.parseArray(content);
                if (conArr != null && conArr.size() > 0) {
                    JSONObject conObj = conArr.getJSONObject(0);
                    String cat = conObj.getString("category");

                    String eleSn = elementService.requireOrCreateElement(templateId, cat);
                    ReviewContent update = new ReviewContent();
                    update.setId(reviewContent.getId());
                    update.setUpdatedTime(new Date());
                    update.setEleId(eleSn);
                    reviewContentMapper.updateById(update);

                    logger.info(">>>>>>> updated null ele ReviewContent, id is <" + eleSn + ">, reviewId is <" + reviewContent.getId() + ">, name is <" + cat + ">");

                    count++;
                }

                ReviewContent update = new ReviewContent();
                update.setUpdatedTime(new Date());
                update.setId(reviewContent.getId());
                reviewContentMapper.updateById(update);*/
            }
        }

        return ">>>>> finished " + count + " review content....";
    }

}
